package ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;

/** 
 * @author Matthias
 * @author Niels Christian Mouritsen
 */
public class Welcome extends JPanel{
	
	private final JLabel lbl_Welcome;
	private final JButton btn_Login;
	private final JTextField textField_ID;
	private final JLabel lbl_ID;
	private final JLabel lbl_PIN;
	private final JPasswordField passwordField_PIN;
	private final JButton btn_Exit;
	private final JLabel lbl_Time;
	private final JLabel lbl_TimeOfLogin;
	private final JLabel logoATM;
	
	/** declaring an int for the type userID. **/
	private int userID;
	/** declaring an int for type userPIN. **/
	private int userPIN;
	
	/** 
	 * Contains the information/blueprint on labels, buttons, 
	 * radio buttons and text fields
	 * Sets the window Dimensions and layout to null 
	 */
	public Welcome() {
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);
		
		lbl_Welcome = new JLabel("Welcome to the ATM");
		lbl_Welcome.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 24));
		lbl_Welcome.setBounds(180, 25, 261, 32);
		add(lbl_Welcome);
		
		btn_Login = new JButton("Login");
		btn_Login.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Login.setBounds(371, 330, 130, 50);
		add(btn_Login);
		
		textField_ID = new JTextField();
		((AbstractDocument)textField_ID.getDocument()).setDocumentFilter(new LimitOfChar(8));
		textField_ID.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_ID.setBounds(201, 179, 190, 30);
		add(textField_ID);
		textField_ID.setColumns(10);
		
		lbl_ID = new JLabel("ID");
		lbl_ID.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_ID.setBounds(126, 186, 46, 14);
		add(lbl_ID);
		
		lbl_PIN = new JLabel("PIN");
		lbl_PIN.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_PIN.setBounds(126, 227, 46, 14);
		add(lbl_PIN);
		
		passwordField_PIN = new JPasswordField();
		((AbstractDocument)passwordField_PIN.getDocument()).setDocumentFilter(new LimitOfChar(4));
		passwordField_PIN.setFont(new Font("SansSerif", Font.PLAIN, 16));
		passwordField_PIN.setBounds(201, 220, 190, 30);
		add(passwordField_PIN);
		
		btn_Exit = new JButton("Exit");
		btn_Exit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Exit.setBounds(99, 330, 130, 50);
		add(btn_Exit);
		
		Date dateAndTime = Calendar.getInstance().getTime();
		lbl_Time = new JLabel(dateAndTime.toString());
		lbl_Time.setFont(new Font("SansSerif", Font.BOLD, 14));
		lbl_Time.setBounds(180, 113, 261, 19);
		add(lbl_Time);
		
		lbl_TimeOfLogin = new JLabel("Time of Login");
		lbl_TimeOfLogin.setFont(new Font("SansSerif", Font.BOLD, 14));
		lbl_TimeOfLogin.setBounds(234, 81, 104, 21);
		add(lbl_TimeOfLogin);
		
		logoATM = new JLabel("");
		logoATM.setIcon(new ImageIcon(UserMenuDelete.class.getResource("/pic/LOGO Mini.png")));
		logoATM.setBounds(15, 15, 50, 50);
		add(logoATM);
	}
	
	/** Adds action listeners, in order to define an action 
	 * when a component is called
	 * @param event		having a ActionListener
	 */
	public final void addActionListener(final ActionListener event) {
		btn_Login.addActionListener(event);
		btn_Exit.addActionListener(event);
	}

	/**
	 * clearing all fields, and setting userID and userPIN 
	 * to be 0, in order to reset the previous entered data
	 * avoiding a login with the previous user
	 */
	public final void clearFields() {
		textField_ID.setText("");
		passwordField_PIN.setText("");
		userID = 0;
		userPIN = 0;
	}
	
	/**
	 * @return button exit
	 * closing the window, and thereby the connection to the DB
	 */
	public final JButton getBtnExit() {
		return btn_Exit;
	}
	
	/**
	 * @return entered user id in the text field of the ui
	 */
	public final int getLoginUserID() {
		try {
			userID = Integer.parseInt(textField_ID.getText().trim()); 	
		} catch (Exception exc) {
		}
		return userID;
	}
	
	/**
	 * @return entered pin in the password field of the ui
	 */
	public final int getLoginPin() {
		try {
			char[] pin = passwordField_PIN.getPassword();
			userPIN = Integer.parseInt(new String(pin));
		} 
		// no statements, since the statement, in ATM is calling
		// @see controller.ATM#authenticateUser()
		catch (Exception exc) {}
		return userPIN;
	}
	
} //end class